/**
 * 
 */
package br.skytef.stmanager.daos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import br.skytef.stmanager.entities.SoftwareHouseEntity;
import br.skytef.stmanager.utils.Pool;

/**
 * @author Ivam Henrique Marques dos Santos
 *         email: ivam.santos@skytef.com.br
 *         skype: ivam.santos.skytef
 *         telefone: +55 11 96214-7543
 */
public class SoftwareHouseDao {

	/**
	 * Variável para conexão, obtido através de um Pool(br.skytef.stmanager.utils.Pool)
	 */
	private static Connection conexao = null;	
	/**
	 * Inserir
	 * @param SoftwareHouse
	 */
	public void inserir(SoftwareHouseEntity house){
		try {
			conexao = Pool.getConnection();
			String query = "INSERT INTO SoftwareHouse(nome,cliente_idCliente)"
					+ "VALUES(?,?);";
			PreparedStatement preparedStmt = conexao.prepareStatement(query);
			preparedStmt.setString				(1,house.getNome().toString());	
			preparedStmt.setInt					(2, house.getId_idCLiente());
			preparedStmt.execute();		
			conexao.close();

		} catch (SQLException e) {
			System.out.println("SQL erro: "+ e);
		}catch (ClassCastException e) {
			System.out.println("Classe erro: "+ e);
		} catch (Exception e) {
			System.out.println("Geral erro: "+ e);
		}
	}	


	public SoftwareHouseEntity buscarIdCliente(int idCliente){
		SoftwareHouseEntity house = null;
		try	{
			conexao = Pool.getConnection();
			Statement st = conexao.createStatement();
			ResultSet rs = st.executeQuery("SELECT * FROM SoftwareHouse WHERE cliente_idCliente = "+ idCliente +";");
			if (rs.next()) {
				do {    
					house = new SoftwareHouseEntity();
					house.setNome(rs.getString("nome"));
					house.setId_idCLiente(rs.getInt("cliente_idCliente"));

				} while (rs.next());
			}
			conexao.close();			
		}catch (SQLException e) {
			System.out.println("SQL erro: "+ e);
		} catch (Exception e) {
			System.out.println("Geral erro: "+ e);
		} 
		return house;

	}

	public void alterar(SoftwareHouseEntity house, int idCliente){
		try {
			conexao = Pool.getConnection();

			String query = "UPDATE SoftwareHouse SET "+
					"nome=?"
					+" WHERE cliente_idCliente=?";
			PreparedStatement preparedStmt = conexao.prepareStatement(query);
			preparedStmt.setString 		(1, house.getNome());
			preparedStmt.setInt 		(2, idCliente);
			preparedStmt.execute();			
			conexao.close();

		} catch (SQLException e) {
			System.out.println("SQL erro: "+ e);
		} catch (Exception e) {
			System.out.println("Geral erro: "+ e);
		}
	}

}